<!DOCTYPE html>
<html>
  <head><meta name="generator" content="Hexo 3.8.0">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="火骑小龙龙づ博客 vue mint-ui js html css 前端">
  <meta name="keyword" content="hexo-theme, vuejs">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      ElementUI+Vue踩坑集合 | 火骑小龙龙づ博客
    
  </title>
  <link href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdn.bootcss.com/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdn.bootcss.com/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="/css/plugins/gitment.css">
  <script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdn.bootcss.com/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdn.bootcss.com/nprogress/0.2.0/nprogress.min.js"></script>
  <script src="/js/qrious.js"></script>
<script src="/js/gitment.js"></script>
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


</head>
<div class="wechat-share">
  <img src="/css/images/logo.png">
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>火骑小龙龙づ博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">Home</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags" class="item-link">Tags</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/categories" class="item-link">Categories</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives" class="item-link">Archives</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/project" class="item-link">Project</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about" class="item-link">About</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">Home</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags" class="menu-link">Tags</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/categories" class="menu-link">Categories</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives" class="menu-link">Archives</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/project" class="menu-link">Project</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about" class="menu-link">About</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>


    <div id="article-banner">
  <h2>ElementUI+Vue踩坑集合</h2>
  <p class="post-date">2019-09-24</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><p>工作中使用Vue + ELementUi踩坑记录</p>
<a id="more"></a>
<blockquote>
<p>常见问题</p>
</blockquote>
<ol>
<li><p>给组件绑定的事件为什么无法触发？</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 在 Vue 2.0 中，为自定义组件绑定原生事件必须使用 .native 修饰符: */</span></span><br><span class="line">&lt;my-component @click.native=<span class="string">"handleClick"</span>&gt;Click Me&lt;<span class="regexp">/my-component&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">/</span>* </span><br><span class="line">    从易用性的角度出发，我们对 Button 组件进行了处理，使它可以监听 click 事件 </span><br><span class="line">    el-button 在组件内部采用 <span class="keyword">this</span>.$emit(<span class="string">'click'</span>)</span><br><span class="line">*<span class="regexp">/</span></span><br><span class="line"><span class="regexp">&lt;el-button @click="handleButtonClick"&gt;Click Me&lt;/</span>el-button&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* el-button实现原理 */</span></span><br><span class="line">&lt;button @click=<span class="string">"handleClick"</span> /&gt;</span><br><span class="line">methods: &#123;</span><br><span class="line">    handleClick() &#123;</span><br><span class="line">        <span class="keyword">this</span>.$emit(<span class="string">'click'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>如何在 Table 组件的每一行添加操作该行数据的按钮？</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 使用 Scoped slot 即可：参数 row 即为对应行的数据。 */</span></span><br><span class="line">&lt;el-table-column label=<span class="string">"操作"</span>&gt;</span><br><span class="line">  &lt;template slot-scope=<span class="string">"props"</span>&gt;</span><br><span class="line">    &lt;el-button @click.native=<span class="string">"showDetail(props.row)"</span>&gt;查看详情&lt;<span class="regexp">/el-button&gt;</span></span><br><span class="line"><span class="regexp">  &lt;/</span>template&gt;</span><br><span class="line">&lt;<span class="regexp">/el-table-column&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<blockquote>
<p>工作中问题总结</p>
</blockquote>
<ol>
<li>form表单校验<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*  </span></span><br><span class="line"><span class="comment">    form表单提交会有很多的字段属性，而且在提交后需要重置为初始状态，</span></span><br><span class="line"><span class="comment">    很多小伙伴为了方便会直接给form=&#123;&#125;,看似达到初始化的目的，</span></span><br><span class="line"><span class="comment">    但会再次出发ElementUi中form表单的校验，特别是在dialog中，</span></span><br><span class="line"><span class="comment">    如果你在opend方法中采用form=&#123;&#125;,你就会发现，</span></span><br><span class="line"><span class="comment">    弹出层一显示就会在form上出现form检验失败的红字警告信息，</span></span><br><span class="line"><span class="comment">    正确的做法还是规规矩矩form.name = "" </span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">&lt;select v-model=<span class="string">"form.name"</span>&gt;&lt;/select&gt;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<blockquote>
<p>业务技巧</p>
</blockquote>
<ol>
<li><p>ElementUI 中日期选择器 常见限制可选择的日期</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">el-form-item</span> <span class="attr">label</span>=<span class="string">"日期"</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">el-date-picker</span> <span class="attr">type</span>=<span class="string">"date"</span> <span class="attr">value-format</span>=<span class="string">"yyyy-MM-dd"</span> <span class="attr">v-model</span>=<span class="string">"BirthTm"</span> <span class="attr">style</span>=<span class="string">"width: 140%;"</span>  <span class="attr">:picker-options</span>=<span class="string">"dateLimit"</span>&gt;</span><span class="tag">&lt;/<span class="name">el-date-picker</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">el-form-item</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="undefined">    data ()&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">/* 可选择今天（今天之后）的日期 */</span></span></span><br><span class="line"><span class="undefined">            dateLimit: &#123;</span></span><br><span class="line"><span class="undefined">                disabledDate(time) &#123;</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">return</span> time.getTime() &lt; <span class="built_in">Date</span>.now() - <span class="number">8.64e7</span>; <span class="comment">// - 8.64e7 表示可选择当天时间</span></span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="undefined">            &#125;,</span></span><br><span class="line"><span class="javascript">            <span class="comment">/* 可选择今天（今天之前）的日期 */</span></span></span><br><span class="line"><span class="undefined">            dateLimit: &#123;</span></span><br><span class="line"><span class="undefined">                disabledDate(time) &#123;</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">return</span> time.getTime() &gt; <span class="built_in">Date</span>.now() - <span class="number">8.64e6</span> <span class="comment">// - 8.64e7 表示可选择当天时间</span></span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="undefined">            &#125;, </span></span><br><span class="line"><span class="javascript">            <span class="comment">/* 可选择2019-04-01 -- 今天（今天之前）的日期  */</span></span></span><br><span class="line"><span class="undefined">            dateLimit: &#123;</span></span><br><span class="line"><span class="undefined">                disabledDate(time) &#123;</span></span><br><span class="line"><span class="javascript">                    <span class="keyword">return</span>  time.getTime() &gt; <span class="built_in">Date</span>.now() || time.getTime() &lt; <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">'2019-04-01'</span>) <span class="number">-8.64e7</span>  ;</span></span><br><span class="line"><span class="undefined">                &#125;</span></span><br><span class="line"><span class="undefined">            &#125;,  </span></span><br><span class="line"><span class="undefined">        &#125;     </span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>优化浏览器scroll滚动条 <code>el-scrollbar</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">'scroll-wrap'</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">el-scrollbar</span> <span class="attr">class</span>=<span class="string">'scroll-bar'</span>&gt;</span><span class="tag">&lt;/<span class="name">el-scrollbar</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">    注意： scroll-bar 必须要设置固定的高度才可以滚动，或者可以scroll-wrap相对定位，scroll-bar绝对定位</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>table列的条件筛选</p>
</li>
</ol>
<p><img src="https://taoaiyi1108.oss-cn-beijing.aliyuncs.com/post/element/elemntui-table-filter.png" alt=""></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">el-table</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">:data</span>=<span class="string">"data"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">...</span></span></span><br><span class="line"><span class="tag">    @<span class="attr">filter-change</span>=<span class="string">"change"</span></span></span><br><span class="line"><span class="tag">&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">el-table-colum</span></span></span><br><span class="line"><span class="tag">        <span class="attr">...</span></span></span><br><span class="line"><span class="tag">        <span class="attr">:colum-key</span>=<span class="string">"num"</span></span></span><br><span class="line"><span class="tag">    &gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">el-tabel</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="undefined">method()&#123;</span></span><br><span class="line"><span class="undefined">    change(filter)&#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log(filter)</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined">&#125;</span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure></section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#Element">
    <span class="tag-code">Element</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2019/09/20/biancheng/Next/NextJs/">
        <span class="nav-arrow">← </span>
        
          NextJs
        
      </a>
    
    
      <a class="nav-right" href="/2019/09/25/biancheng/OAuth/OAuth-认证/">
        
          OAuth 认证
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- No Comment -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://taoaiyi1108.github.io/2019/09/24/biancheng/Element/ElementUI-Vue踩坑集合/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

     // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()
        
        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })

    // qrcode
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });

    // gitment
    var gitmentConfig = "taoaiyi1108";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "ElementUI+Vue踩坑集合",
        owner: "taoaiyi1108",
        repo: "taoaiyi1108.github.io",
        oauth: {
          client_id: "0f87e490e00ee3fd87ef",
          client_secret: "4a9d2b148e7971c2201ad12131ce8bf8159ccd2e"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  })();
</script>

<script>
  var disqus_shortname = '';
  
  var disqus_url = 'http://taoaiyi1108.github.io/2019/09/24/biancheng/Element/ElementUI-Vue踩坑集合/';
  
  (function(){
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//go.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>

    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    &copy; 2019 火骑小龙龙 | Proudly powered by <a href="https://hexo.io" target="_blank">Hexo</a>
    <br>
    感谢大神 <a href="https://github.com/yanm1ng" target="_blank">yanm1ng</a> 对本博客的技术支持
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->

<script src="/js/script.js"></script>
  </body>
</html>